﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KundeWeb.KundeService;

namespace KundeWeb
{
	public partial class _default : System.Web.UI.Page
	{
		KundeServiceClient service;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (Session["service"] == null)
			{
				service = new KundeServiceClient();
				Session["service"] = service;
			}
			else
			{
				service = (KundeServiceClient)Session["service"];
			}

			if (Session["kunde"] != null)
			{
				KundeDTO kunde = (KundeDTO)Session["kunde"];
				lblKundenavn.Text = "Navn: " + kunde.Navn;
				lblKundenavn.Visible = true;

				Ordre[] ordrer = service.HentOrdrer();

				foreach (Ordre ordre in ordrer)
				{
					ListItem l = new ListItem(ordre.Dato.ToShortDateString(), ordre.Delordreliste[0].Delordrenr.ToString());
					ddlOrdrer.Items.Add(l);
				}

				HomepartyDTO[] homeparties = service.HentHomeparties();

				foreach (HomepartyDTO homeparty in homeparties)
				{
					ListItem l = new ListItem(homeparty.Dato.ToShortDateString(), homeparty.Id.ToString());
					ddlHomeparties.Items.Add(l);
				}

				ddlOrdrer.Visible = true;
				btnVælg.Visible = true;
				lblOrdreText.Visible = true;
			}

			int id;

			if (int.TryParse(Request.QueryString["id"], out id))
			{
				DelordreDTO delordre = service.HentDelordre(id);
				double totalPris = 0;
				foreach (DelordrelinieDTO delordrelinie in delordre.Delordrelinieliste)
				{
					totalPris += delordrelinie.Pris;
					ListItem l = new ListItem(delordrelinie.Vare.Navn + " - " + delordrelinie.Vare.Beskrivelse + " - Antal: " + delordrelinie.Antal + " - Pris: " + delordrelinie.Pris + "kr");
					lstVarer.Items.Add(l);
				}
				lstVarer.Visible = true;
				lblTotalPris.Text = "Total pris: " + totalPris + "kr";
			}

			if (int.TryParse(Request.QueryString["homeparty"], out id))
			{
				HomepartyDTO homeparty = service.HentHomeparty(id);
				lblSælgerNavn.Text = "Sælger: " + homeparty.Navn;
				lblMail.Text = "E-mail: " + homeparty.Mail;
				lblTlf.Text = "Tlf: " + homeparty.Tlf;
				lblTid.Text = "Dato: " + homeparty.Dato.Day + "/" + homeparty.Dato.Month + "/" + homeparty.Dato.Year + " " + homeparty.Dato.Hour + ":" + homeparty.Dato.Minute;
				lblSælgerNavn.Visible = true;
				lblMail.Visible = true;
				lblTlf.Visible = true;
				lblTid.Visible = true;
			}
		}

		protected void btnSøgKunde_Click(object sender, EventArgs e)
		{
			int kundeNr;
			bool kundeNrCheck = int.TryParse(txtKundenr.Text, out kundeNr);

			if (kundeNrCheck)
			{
				KundeDTO kunde;

				if (Session["kunde"] != null)
				{
					kunde = (KundeDTO) Session["kunde"];
				}
				else
				{
					kunde = service.FindesKunde(kundeNr);
				}
				if (kunde != null)
				{
					Session["kunde"] = kunde;

					lblKundenavn.Text = "Navn: " + kunde.Navn;

					Ordre[] ordrer = service.HentOrdrer();
					foreach (Ordre ordre in ordrer)
					{
						ListItem l = new ListItem(ordre.Dato.ToShortDateString(), ordre.Delordreliste[0].Delordrenr.ToString());
						ddlOrdrer.Items.Add(l);
					}
					HomepartyDTO[] homeparties = service.HentHomeparties();
					foreach (HomepartyDTO homeparty in homeparties)
					{
						ListItem l = new ListItem(homeparty.Dato.ToShortDateString(), homeparty.Id.ToString());
						ddlHomeparties.Items.Add(l);
					}

					btnVælg.Visible = true;
					ddlOrdrer.Visible = true;
					lblOrdreText.Visible = true;
				}
				else
				{
					Response.Write("<script text=\"text/javascript\">alert('Der findes ikke en kunde med det kundenr');</script>");
				}
			}
			else
			{
				Response.Write("<script text=\"text/javascript\">alert('Kunde nr skal være nummerisk');</script>");
			}
		}

		protected void btnVælg_Click(object sender, EventArgs e)
		{
			Response.Redirect("?id=" + ddlOrdrer.SelectedValue);
		}

		protected void btnVælgHomeparty_Click(object sender, EventArgs e)
		{
			Response.Redirect("?homeparty=" + ddlHomeparties.SelectedValue);
		}
	}
}